package com.webguard.outlierdetection.controller;

import com.webguard.outlierdetection.pojo.Result;
import com.webguard.outlierdetection.service.LLMService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.RejectedExecutionException;

@RestController
@RequestMapping("/llm")
public class LLMController {
    private static final Logger logger = LoggerFactory.getLogger(LLMController.class);

    @Autowired
    private LLMService llmService;

    @PostMapping("/process")
    public ResponseEntity<Result> processTraffic(@RequestBody String rawData) {
        try {
            llmService.submitRawData(rawData);
            return ResponseEntity.accepted()
                    .body(Result.success("数据已接收处理"));
        } catch (RejectedExecutionException e) {
            logger.warn("System overload: {}", rawData);
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS)
                    .body(Result.error("系统繁忙，请稍后重试"));
        }
    }
    @PostMapping("/analyze")
    public ResponseEntity<Result<String>> analyzeRawData(
            @RequestParam("rawdata") String rawData) { // 使用@RequestParam接收参数
        try {
            return ResponseEntity.ok(llmService.analyzeRawData(rawData));
        } catch (Exception e) {
            logger.error("分析流程异常: {}", rawData, e);
            return ResponseEntity.internalServerError()
                    .body(Result.error("分析服务不可用"));
        }
    }

}
